<?php
class ControllerCommonHome extends Controller {
	
	public function index() {
		$this->load->language('common/home');
		
		$this->document->title = $this->language->get('heading_title');
		
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_left_overview'] = $this->language->get('text_left_overview');
		$this->data['text_right_overview'] = $this->language->get('text_right_overview');
		$this->data['text_current_time'] = $this->language->get('text_current_time');
		$this->data['text_login_username'] = $this->language->get('text_login_username');
		$this->data['text_registered_time'] = $this->language->get('text_registered_time');
		$this->data['text_last_login_time'] = $this->language->get('text_last_login_time');
		$this->data['text_client_ip'] = $this->language->get('text_client_ip');
		
		
		$this->data['menu_admininfo'] = $this->language->get('menu_admininfo');
		$this->data['menu_contentmgt'] = $this->language->get('menu_contentmgt');
		
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_edit'] = $this->language->get('button_edit');
		
		$this->data['currentusername'] = $this->user->getUserName();
		$this->data['registeredtime'] = $this->user->getRegisteredTime();
		$this->data['lastlogintime'] = $this->user->getLastLoginTime();
		$this->data['clientip'] = $this->user->getClientIP();
		
		$this->data['menu_admininfo_link'] = HTTPS_SERVER . 'index.php?route=admininfo/user&token=' . $this->session->data['token'];
		$this->data['menu_contentmgt_link'] = HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'];
		$this->data['admininfo_insert_link'] = HTTPS_SERVER . 'index.php?route=admininfo/user/insert&token=' . $this->session->data['token'];
		$this->data['admininfo_update_link'] = HTTPS_SERVER . 'index.php?route=admininfo/user/update&token=' . $this->session->data['token'] . '&user_id=' . $this->user->getId();
		
		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);
		
		$this->data['token'] = $this->session->data['token'];
		
		$this->template = 'common/home.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	
	public function login() {
		if (!$this->user->isLogged()) {
			return $this->forward('common/login');
		}

		if (isset($this->request->get['route'])) {
			$route = '';

			$part = explode('/', $this->request->get['route']);
				
			if (isset($part[0])) {
				$route .= $part[0];
			}
				
			if (isset($part[1])) {
				$route .= '/' . $part[1];
			}
				
			$ignore = array(
				'common/login',
				'common/logout',
				'error/not_found',
				'error/permission'
				);
					
				$config_ignore = array();
					
				if ($this->config->get('config_token_ignore')) {
					$config_ignore = unserialize($this->config->get('config_token_ignore'));
				}

				$ignore = array_merge($ignore, $config_ignore);

				if (!in_array($route, $ignore)) {
					if (!isset($this->request->get['token']) || !isset($this->session->data['token']) || ($this->request->get['token'] != $this->session->data['token'])) {
						return $this->forward('common/login');
					}
				}
		} else {
			if (!isset($this->request->get['token']) || !isset($this->session->data['token']) || ($this->request->get['token'] != $this->session->data['token'])) {
				return $this->forward('common/login');
			}
		}
	}
	public function permission() {
		if (isset($this->request->get['route'])) {
			$route = '';
				
			$part = explode('/', $this->request->get['route']);
				
			if (isset($part[0])) {
				$route .= $part[0];
			}
				
			if (isset($part[1])) {
				$route .= '/' . $part[1];
			}
				
			$ignore = array(
				'common/home',
				'common/login',
				'common/logout',
				'error/not_found',
				'error/permission',	
				'error/token'		
				);

				if (!in_array($route, $ignore)) {
					if (!$this->user->hasPermission('access', $route)) {
						return $this->forward('error/permission');
					}
				}
		}
	}
}
?>